import pymysql

try:
    connect = pymysql.connect(
        host = '119.45.203.8', # 主机名
        user = 'root',      # 数据库用户名
        password = 'zsy123456',  # 数据库密码
        db = 'pythondemo',      # 数据库名
        charset = 'utf8',   # 字符集编码
        cursorclass = pymysql.cursors.DictCursor, # 游标类型,
        port = 3301  # 端口
   )
except Exception as e:
    print(e)
try:
    # 获取游标对象
    with connect.cursor() as cursor:
        # SQL语句
        sql = 'select * from goods where price > 3000'

        # 执行SQL语句
        cursor.execute(sql)

        # 查找一条数据
        # result = cursor.fetchone()

        # 查找多条数据
        result = cursor.fetch

        # 查找数据,获取结构集的所有记录
        result = cursor.fetchall()

except Exception as e:
    print(e)
finally:
    # 关闭连接
    connect.close()

# print(result)

for goods in result:
    print(f'商品名称：{goods["name"]} , 价格:{goods["price"]}元')




